<?php
/*+------------------------------------+
  |      站点配置管理                  |
  +------------------------------------+*/
class WebsiteConfigManageAction extends CommonAction{
      /*+------------------------------+
        |       本站配置管理           |
        +------------------------------+*/
      public function websiteConfig(){
             if($_POST['save']){
                   $_POST['config_info'][2] = str_replace('\"','"',$_POST['config_info'][2]);
                   $data   = $this->resetConfigArray($_POST);
                   $this->changeDB();
                   $config = D("website_config");
             
                   foreach($data as $key =>$val){
                         $save += $config ->save($val);
                   }
                   if($save){
                         $this->assign("jumpUrl",U("WebsiteConfigManage/websiteConfig/",array("config"=>$_POST['column_id'],"column_id"=>$_POST['column_id'])));
                         $this->success("配置成功！");
                   }else{
                         $this->assign("jumpUrl",U("WebsiteConfigManage/websiteConfig/",array("config"=>$_POST['column_id'],"column_id"=>$_POST['column_id'])));
                         $this->error("配置失败！");
                   }
             }else{
                   $this->changeDB();
                   $config = D("WebsiteConfig");
                   $this->assign("websiteConfig",$config ->getColumnConfig(0,1));    
                   $this->display();
             }
      }

      /*+------------------------------+
        |       上传文件管理           |
        +------------------------------+*/
      public function fileUpload(){
             //获取栏目信息
             $column = $this->getColumnList();
             $this   ->assign("columnName",$column);
             if($_GET['config']){
                 $this->assign("column_id",$_GET['column_id']);
                 //获取该站点该栏目下的文件上传配置
                 $config = D("WebsiteConfig");
                 //echo "<pre>";print_r($config ->getColumnConfig($_POST['column_id']));
                 //$this->assign("fileUploadConfig",$config ->getColumnConfig($_POST['column_id']));
                 $this->assign("fileUploadConfig",$config ->getColumnConfig($_GET['column_id'],2));
             }
             if($_POST['save']){
                  $data   = $this->resetConfigArray($_POST);
                 
                  $config = D("website_config");
                  foreach($data as $key =>$val){
                         $save += $config ->save($val);
                  }
                  if($save){
                         $this->assign("jumpUrl",U("WebsiteConfigManage/fileUpload/",array("config"=>$_POST['column_id'],"column_id"=>$_POST['column_id'])));
                         $this->success("配置成功！");
                  }else{
                         $this->assign("jumpUrl",U("WebsiteConfigManage/fileUpload/",array("config"=>$_POST['column_id'],"column_id"=>$_POST['column_id'])));
                         $this->error("配置失败！");
                  }
             }
             $this   ->display();
      }

      //获取除栏目类型为link以外的栏目信息
      private function getColumnList($pid){
              $this    ->changeDB();
              $col     = D("website_column");
              $info    = $col ->where("column_type != 'link'")->select();
              return   $info;
      }

      //重组配置数组
      private function resetConfigArray($param){
              foreach($param as $key => $val){
                     foreach($val as $k => $v){
                            if($key == "id" || $key == "config_info"){
                                    $info[$k][$key]=$v;
                            }
                     }
              }
              return $info;
      }
      /*+------------------------------+
        |       图片水印管理           |
        +------------------------------+*/
      public function watermark(){
             $column = $this->getColumnList();
             $this   ->assign("columnName",$column);
             if($_GET['config']){
                 $this->assign("column_id",$_GET['column_id']);
                 //获取该站点该栏目下的文件上传配置
                 $config = D("WebsiteConfig");
                 //echo "<pre>";print_r($config ->getColumnConfig($_POST['column_id']));
                 //$this->assign("fileUploadConfig",$config ->getColumnConfig($_POST['column_id']));
                 $this->assign("watermarkConfig",$config ->getColumnConfig($_GET['column_id'],3));
             }
             if($_POST['save']){
                 //echo "<pre>";
                 //print_r($_FILES);exit;
                  if(!empty($_FILES['Image']['name'])){
                      //获取图片上传配置
                     
                      $uploadConfig = $this->uploadConfig($_POST['column_id']);
                      foreach($uploadConfig as $key => $val){
                              foreach($val as $k => $v){
                                  if($v == "允许上传大小"){
                                         $watermark['maxSize']   = $uploadConfig[$key]['config_info'];
                                  }
                                  if($v == "允许上传类型"){
                                         $watermark['allowExts'] = $uploadConfig[$key]['config_info'];
                                  }
                              }
                      }
                      $watermark['allowExtsExplode'] = "|";
                      $watermark['thumbPrefix']      = "w_";
                      //成缩略图
                      
                      $watermarkSize                 = explode("x",$_POST['config_info'][1]);
                      $watermark['thumbMaxWidth']    = $watermarkSize[0];
                      $watermark['thumbMaxHeight']   = $watermarkSize[1];
                      if($_POST['config_info'][0] == "1"){
                                $fileUploadResult              = $this->_upload($watermark);
                                $_POST['config_info'][2]       = date('Y')."/".date("m")."/".date("d")."/w_".$fileUploadResult[0]['savename'];
                                $resoure['tableName'] = "website_config";
                                $resoure['id']        = $_POST['id'][2];
                                $resoure['field']     = "config_info";
                                $resoure['prefix']    = __ROOT__."/Data/Admin/Uploads/";
                                //修改水印图片时，删除旧的水印图片
                                $unlink               = $this->trashRecycling($resoure);
                                if($unlink){
                                     $massage = "，旧的的水印图片已被删除！";
                                }
                      }
                  }
                  $data = $this->resetConfigArray($_POST);
                  $config = D("website_config");
                  foreach($data as $key =>$val){
                         
                         $save += $config ->save($val);
                  }
                  //exit;
                  if($save){
                         $this->assign("jumpUrl",U("WebsiteConfigManage/watermark/",array("config"=>$_POST['column_id'],"column_id"=>$_POST['column_id'])));
                         $this->success("配置成功{$massage}！");
                  }else{
                         $this->assign("jumpUrl",U("WebsiteConfigManage/watermark/",array("config"=>$_POST['column_id'],"column_id"=>$_POST['column_id'])));
                         $this->error("配置失败！");
                  }
             }
             $this->display();
      }
}
?>
